package com.ruoyi.project.oms.paeaAuType.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.oms.paeaAuPoint.domain.PaeaAuPoint;
import com.ruoyi.project.oms.paeaAuPoint.service.IPaeaAuPointService;
import com.ruoyi.project.oms.paeaAuType.domain.PaeaAuType;
import com.ruoyi.project.oms.paeaAuType.service.IPaeaAuTypeService;
import com.ruoyi.project.oms.paeaAuTypeErrPointRel.service.IPaeaAuTypeErrPointRelService;
import com.ruoyi.project.oms.paeaAuTypePointRel.service.IPaeaAuTypePointRelService;
import com.ruoyi.project.er.domain.PaeaErPoint;
import com.ruoyi.project.er.service.IPaeaErPointService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 监督类型Controller
 *
 * @author 关晓强
 * @date 2020-03-14
 */
@Controller
@RequestMapping("/oms/paeaAuType")
public class PaeaAuTypeController extends BaseController
{
    private String prefix = "oms/paeaAuType";

    @Autowired
    private IPaeaAuTypeService paeaAuTypeService;

    @Autowired
    private IPaeaAuTypePointRelService paeaAuTypePointRelService;

    @Autowired
    private IPaeaAuPointService paeaAuPointService;

    @Autowired
    private IPaeaErPointService paeaErPointService;

    @Autowired
    private IPaeaAuTypeErrPointRelService paeaAuTypeErrPointRelService;

    @RequiresPermissions("oms:paeaAuType:view")
    @GetMapping()
    public String paeaAuType()
    {
        return prefix + "/paeaAuType";
    }

    /**
     * 查询监督类型列表
     */
    @RequiresPermissions("oms:paeaAuType:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaeaAuType paeaAuType)
    {
        startPage();
        List<PaeaAuType> list = paeaAuTypeService.list(new QueryWrapper<>(paeaAuType));
        return getDataTable(list);
    }

    /**
     * 导出监督类型列表
     */
    @RequiresPermissions("oms:paeaAuType:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PaeaAuType paeaAuType)
    {
        List<PaeaAuType> list = paeaAuTypeService.list(new QueryWrapper<>(paeaAuType));
        ExcelUtil<PaeaAuType> util = new ExcelUtil<PaeaAuType>(PaeaAuType.class);
        return util.exportExcel(list, "paeaAuType");
    }

    /**
     * 新增监督类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存监督类型
     */
    @RequiresPermissions("oms:paeaAuType:add")
    @Log(title = "监督类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PaeaAuType paeaAuType)
    {
        paeaAuType.setId(paeaAuType.getCode());
        return toAjax(paeaAuTypeService.saveOrUpdate(paeaAuType));
    }

    /**
     * 修改监督类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaAuType paeaAuType = paeaAuTypeService.getById(id);
        mmap.put("paeaAuType", paeaAuType);
        return prefix + "/edit";
    }

    /**
     * 修改保存监督类型
     */
    @RequiresPermissions("oms:paeaAuType:edit")
    @Log(title = "监督类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaeaAuType paeaAuType)
    {
        return toAjax(paeaAuTypeService.saveOrUpdate(paeaAuType));
    }

    /**
     * 删除监督类型
     */
    @RequiresPermissions("oms:paeaAuType:remove")
    @Log(title = "监督类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(paeaAuTypeService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 校验监督类型代码
     */
    @PostMapping("/checkCodeUnique")
    @ResponseBody
    public String checkCodeUnique(PaeaAuType paeaAuType)
    {
        if(paeaAuTypeService.getOne(new QueryWrapper<>(paeaAuType))!=null){
            return Constants.NOT_UNIQUE;
        }
        return Constants.UNIQUE;
    }

    /**
     * 监督模型分配监督点
     */
    @RequiresPermissions("oms:paeaAuType:paeaAuPointConfig")
    @GetMapping("/paeaAuPointConfig/{auTypeId}")
    public String auPointConfig(@PathVariable("auTypeId") String auTypeId, ModelMap mmap)
    {
        mmap.put("allAuPoints", paeaAuPointService.list(new QueryWrapper<PaeaAuPoint>().orderByAsc("code")));
        mmap.put("assignedAuPoints", paeaAuPointService.list(
                new QueryWrapper<PaeaAuPoint>()
                        .inSql("id", MessageFormat.format("select au_point_id from paea_au_type_point_rel where au_type_id = {0}",auTypeId))
                        .orderByAsc("code")));
        mmap.put("auTypeId",auTypeId);
        return prefix + "/paeaAuTypePaeaAuPointConfig";
    }

    /**
     * 保存监督模型分配监督点
     */
    @RequiresPermissions("oms:paeaAuType:paeaAuPointConfig")
    @Log(title = "监督模型分配监督点", businessType = BusinessType.UPDATE)
    @PostMapping("/paeaAuPointConfigSave/{auTypeId}")
    @ResponseBody
    public AjaxResult auPointConfigSave(@PathVariable("auTypeId") String auTypeId,String ids)
    {
        return AjaxResult.success(paeaAuTypePointRelService.auPointConfigSave(auTypeId,ids));
    }

    /**
     * 监督模型分配差错点
     */
    @RequiresPermissions("oms:paeaAuType:paeaErPointConfig")
    @GetMapping("/paeaErPointConfig/{auTypeId}")
    public String erPointConfig(@PathVariable("auTypeId") String auTypeId, ModelMap mmap)
    {
        mmap.put("allErPoints", paeaErPointService.list(new QueryWrapper<PaeaErPoint>().orderByAsc("code")));
        mmap.put("assignedErPoints", paeaErPointService.list(
                new QueryWrapper<PaeaErPoint>()
                        .inSql("id", MessageFormat.format("select err_point_id from paea_au_type_err_point_rel where au_type_id = {0}","'"+auTypeId+"'"))
                        .orderByAsc("code")));
        mmap.put("auTypeId",auTypeId);
        return prefix + "/paeaAuTypePaeaErPointConfig";
    }

    /**
     * 保存监督模型分配差错点
     */
    @RequiresPermissions("oms:paeaAuType:paeaErPointConfig")
    @Log(title = "监督模型分配差错点", businessType = BusinessType.UPDATE)
    @PostMapping("/paeaErPointConfigSave/{auTypeId}")
    @ResponseBody
    public AjaxResult erPointConfigSave(@PathVariable("auTypeId") String auTypeId,String ids)
    {
        return AjaxResult.success(paeaAuTypeErrPointRelService.erPointConfigSave(auTypeId,ids));
    }
}
